﻿using InSimDotNet.Packets;
using System.ComponentModel;

namespace LFSRecord2.Model.Replay
{
    public class ImportRpdSectionInfo : INotifyPropertyChanged
    {
        /// <summary>
        /// Name of the replay for this section
        /// </summary>
        public string ReplayName { get; set; }

        /// <summary>
        /// The type of replay
        /// </summary>
        public ReplayTypes ReplayType
        {
            get
            {
                if (ReplayName.Length < 3)
                    return ReplayTypes.NoReplay;

                switch (ReplayName.Substring(ReplayName.Length - 3).ToUpperInvariant())
                {
                    case "SPR":
                        return ReplayTypes.Spr;
                    case "MPR":
                        return ReplayTypes.Mpr;
                }
                return ReplayTypes.NoReplay;
            }
        }

        /// <summary>
        /// The total length of the replay (as reported by LFS), in seconds
        /// </summary>
        public double ReplayLength { get; set; }

        public double ProjectStartTime { get; set; }
        public double ProjectEndTime { get; set; }

        double _recordStartTime = 0;
        /// <summary>
        /// The time into the replay where data recording must start, in seconds
        /// </summary>
        public double RecordStartTime
        {
            get { return _recordStartTime; }
            set
            {
                if (value < 0)
                    _recordStartTime = 0;
                else if (ReplayLength > 0 && value > ReplayLength)
                    _recordStartTime = ReplayLength;
                else
                    _recordStartTime = value;

                if (value > RecordEndTime)
                    RecordEndTime = value;

                OnPropertyChanged("RecordStartTime");
            }
        }

        double _recordEndTime = 0;
        /// <summary>
        /// The time into the replay where data recording must end, in seconds
        /// </summary>
        public double RecordEndTime 
        {
            get { return _recordEndTime; }
            set
            {
                if (value < 0)
                    _recordEndTime = 0;
                else if (ReplayLength > 0 && value > ReplayLength)
                    _recordEndTime = ReplayLength;
                else
                    _recordEndTime = value;

                if (value < RecordStartTime)
                    RecordStartTime = value;

                OnPropertyChanged("RecordEndTime");
            }
        }

        /// <summary>
        /// The replay error (result) code for this replay as reported by LFS
        /// </summary>
        public ReplayError ReplayError { get; set; }

        /// <summary>
        /// Number of playes at the start of recording
        /// </summary>
        public int NumPlayers { get; set; }

        /// <summary>
        /// The time that we seek towards, while recording.
        /// In spr that will simply be the end time, but in mpr we must record in steps. CurSeekTime indicates where we are currently are in the stepped seeking process.
        /// </summary>
        public double CurStepSeekTime { get; set; }

        /// <summary>
        /// In case of MPR precision recording, this holds the time we're recording at right now.
        /// </summary>
        public double CurRecordTime { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
    }
}
